package com.xkcoding.mq.kafka.config;

import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.Cluster;

import java.util.Map;

/**
 * 自定义分区
 *
 * @Author Administrator
 * @Date 2022/4/17 0017 17:59
 **/
public class MyPartitioner implements Partitioner {


    /**
     * @param topic      主题
     * @param key        消息的key
     * @param keyBytes   序列化后的key
     * @param value      消息的value
     * @param valueBytes 序列化的value
     * @param cluster    集群元数据
     * @return
     */
    @Override
    public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
        String valueStr = value.toString();
        if (valueStr.contains("hello")) {
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    public void close() {

    }

    @Override
    public void configure(Map<String, ?> map) {

    }
}
